﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
namespace Spike.ContractBuilder
{
    public class ServiceHostContract : IXmlSerializable
    {
        public ServiceHostContract()
        {
            _endpoints = new Dictionary<string, ServiceHostEndpoint>();
        }
        public ServiceHostContract(string name, string fileName)
        {
            _endpoints = new Dictionary<string, ServiceHostEndpoint>();
            _name = name;
            _fileName = fileName;
        }

        private string _fileName;

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _baseAddress = string.Empty; 

        public string BaseAddress
        {
            get { return _baseAddress; }
            set { _baseAddress = value; }
        }


        private Dictionary<string, ServiceHostEndpoint> _endpoints;

        public Dictionary<string, ServiceHostEndpoint> Endpoints
        {
            get { return _endpoints; }
            set { _endpoints = value; }
        }

        public ServiceHostEndpoint[] GetEndpoints()
        {
            ServiceHostEndpoint[] endpoints = new ServiceHostEndpoint[this.Endpoints.Count];
            this.Endpoints.Values.CopyTo(endpoints, 0);

            return endpoints;
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.Read();

            reader.ReadStartElement("ServiceHostContract");
            _name = reader.ReadElementString("Name");
            _fileName = reader.ReadElementString("WSDL");

            reader.ReadStartElement("Endpoints");
            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                string s = reader.ReadOuterXml();

                ServiceHostEndpoint endpoint = SerializationHelper<ServiceHostEndpoint>.Deserialize(s);
                _endpoints[endpoint.Name] = endpoint;
            }


            reader.ReadEndElement();
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("ServiceHostContract");
            writer.WriteElementString("Name", _name);
            writer.WriteElementString("WSDL", _fileName);

            writer.WriteStartElement("Endpoints");
            foreach (ServiceHostEndpoint endpoint in _endpoints.Values)
            {
                writer.WriteRaw(SerializationHelper<ServiceHostEndpoint>.SerializeServiceObject(endpoint));
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
        }

        #endregion
    }
}
